package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.AliYunUtils;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.facebook.drawee.view.SimpleDraweeView;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarSourcePhotoBean;
import rxbus_base.RxBus;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/2/24.
 * 车源图片大图
 */

public class BigCarSourcePhotoActivity extends BaseActivity {
    @InjectView(R.id.iv_return)
    ImageView ivReturn;
    @InjectView(R.id.fresco_big_photo)
    SimpleDraweeView frescoBigPhoto;

    private List<CarSourcePhotoBean> photoBeanList;
    private String photoUrl;
    private int index;
    private String from;
    private String fileName;
    private String sourceId;
    private String classId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_car_source_photo);
        ButterKnife.inject(this);
        photoBeanList = (List<CarSourcePhotoBean>) getIntent().getSerializableExtra("photoSrouce");
        index = getIntent().getIntExtra("photoIndex", 0);
        photoUrl = getIntent().getStringExtra("photoUrl");
        from = getIntent().getStringExtra("from");
        fileName = getIntent().getStringExtra("fileName");
        sourceId = getIntent().getStringExtra("sourceId");
        classId = getIntent().getStringExtra("classId");
        frescoBigPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BigCarSourcePhotoActivity.this, ScaleBigPhotoActivity.class);
                if (photoBeanList != null) {
                    intent.putExtra("imgUrls", photoBeanList.get(index));
                } else if (!TextUtils.isEmpty(photoUrl)) {
                    intent.putExtra("imgUrls", photoUrl);
                }
                startIntent(intent, false);
            }
        });
        initViews();
    }

    private void initViews() {
        if (photoBeanList != null) {
                if (!TextUtils.isEmpty(photoBeanList.get(index).getPhoto())) {
                    setFrescoImage(photoBeanList.get(index).getPhoto());
                }
        } else if (!TextUtils.isEmpty(photoUrl)) {
            setFrescoImage(photoUrl);
        }
    }
    private void setFrescoImage(String photo) {
        if (photo.contains("http")) {
            frescoBigPhoto.setImageURI(photo);
        } else {
            frescoBigPhoto.setImageURI("file://" + photo);
        }
    }

    @OnClick({R.id.iv_return, R.id.tv_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.tv_delete:
                if (!TextUtils.isEmpty(from)) {
                    backTo(from);
                }
                deleteALiYunFile();
                deleteServiceFile();
                break;
        }
    }

    private void deleteServiceFile() {
        HashMap<String, Object> build = new MapUtil()
                .put("id", CustomSharedPreferences.getString(BigCarSourcePhotoActivity.this, KeyBean.CARID.valueOf(), KeyBean.FILE_NAME.valueOf()))
//              .put("class_id", classId)
                .put("attr_id", sourceId)
                .put("img_info", fileName)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(BigCarSourcePhotoActivity.this, build).create(IApiActivityRequest.class).deleteFile()
                , new BaseSubscriber<TokenBean>(BigCarSourcePhotoActivity.this) {
                    @Override
                    public void onSuccess(TokenBean o) {
                        RxBus.getInstance().sendData(sourceId, null);
                        finish();
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        RxBus.getInstance().sendData(sourceId, null);
                        finish();
                    }
                });
    }

    private void deleteALiYunFile() {
        AliYunUtils.deleteFile(this, fileName);
    }

    private void backTo(String from) {
        switch (from) {
            case "CarSurfacePhotoFragment":
                RxBus.getInstance().sendData("" + RequestAndResultCode.BIGCARSOURCEPHOTOACTIVITY_BACK_CARSURFACEPHOTOFRAGMENT_RESULT, null);
                finish();
                break;
            case "CarProceduresFragment":
                RxBus.getInstance().sendData("" + RequestAndResultCode.BIGCARSOURCEPHOTOACTIVITY_BACK_CARPROCEDURESFRAGMENT_RESULT, null);
                finish();
                break;
            case "CarOrnamentsPhotoFragment":
                RxBus.getInstance().sendData("" + RequestAndResultCode.BIGCARSOURCEPHOTOACTIVITY_BACK_CARORNAMENTSPHOTOFRAGMENT_RESULT, null);
                finish();
                break;
            case "CarEngineFragment":
                RxBus.getInstance().sendData("" + RequestAndResultCode.BIGCARSOURCEPHOTOACTIVITY_BACK_CARENGINEFRAGMENT_RESULT, null);
                finish();
                break;
        }
    }
}
